import Vue from 'vue'
import router from './router'
import store from './store'
import moment from 'moment'
import { ToastPlugin, LoadingPlugin, ConfirmPlugin } from 'vux'
import VueLazyLoad from 'vue-lazyload'
import 'vue2-animate/dist/vue2-animate.min.css'
import 'font-awesome/css/font-awesome.css'
import '@/assets/icon/iconfont.css'
import '@/assets/css/other.css'
import VueTouch from 'vue-touch'
import App from './App'
import VueI18n from 'vue-i18n'
import en from '../static/lang/en'
import zh from '../static/lang/zh'

import { localUser } from '@/assets/js/local'
import pageConf from '@/assets/js/pageConf'
import nativeBridge from '@/assets/js/nativeBridge'
import noImg from '@/assets/img/no_image.jpg'
import loadingImg from '@/assets/img/loading_img.jpg'
import { personifyTime } from '@/assets/js/utils'

// let i = 0
Vue.use(VueLazyLoad, {
  // filter: {
  //   progressive(listener, options) {
  //     listener.loading = i % 2 === 0 ? loading1 : loading2
  //     i++
  //   }
  // },
  error: noImg,
  loading: loadingImg
})

moment.locale('zh-cn')

const FastClick = require('fastclick')
FastClick.attach(document.body)

Vue.prototype.moment = moment
Vue.config.productionTip = false
Vue.use(LoadingPlugin)
Vue.use(ToastPlugin)
Vue.use(ConfirmPlugin)
Vue.use(VueTouch, { name: 'v-touch' })
Vue.use(VueI18n)

Vue.filter('personifyTime', (timeStr) => {
  if (!timeStr) return ''
  return personifyTime(timeStr)
})

router.beforeEach((to, from, next) => {
  // 如果没有登录，则跳转到登录界面
  if (to.name === 'User' && localUser.get('id') == null) {
    next('/login')
    return
  }

  next()
})
router.afterEach((to, from) => {
  const name = to.name
  nativeBridge.handleNativeBack()
  if (name == null || pageConf[name] == null) {
    // 设置默认 (FIXME  IOSBack有问题)
    nativeBridge.setStatusBar(pageConf._default.statusBarColor)
    // nativeBridge.toggleIOSBack(pageConf._default.disableIOSBack)
  } else {
    nativeBridge.setStatusBar(
      pageConf[name].statusBarColor || pageConf._default.statusBarColor
    )
    // nativeBridge.toggleIOSBack(pageConf[name].disableIOSBack || pageConf._default.disableIOSBack)
  }
})

const lang = nativeBridge.getLang()
const i18n = new VueI18n({
  locale: lang,
  messages: {
    zh,
    en
  }
})
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  i18n,
  template: '<App/>',
  components: { App }
})
